<template>
  <div>
    <!--个人信息修改-->
    <p>{{form.codeType}}</p>
    <content-header title="修改资料"></content-header>
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名：">
        <el-input v-model="form.userId" disabled=""></el-input>
      </el-form-item>
      <el-form-item label="真实姓名：" prop="userName">
        <el-input v-model="form.userName"></el-input>
      </el-form-item>
      <el-form-item label="证件类型：" prop="codeType">
        <el-radio-group v-model="form.codeType">
          <el-radio v-for="item in cardTypes" :label="item.dictName" :key="item.id"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.codeType == '身份证'" label="证件号码：" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item v-else label="证件号码：" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="手机号码：" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="年龄：" prop="age">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
      <el-form-item label="性别：">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="职业：">
        <el-input v-model="form.job"></el-input>
      </el-form-item>
      <el-form-item label="QQ：" prop="qq">
        <el-input v-model="form.qq"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="btn">
          <el-button v-loading="loading" type="primary" @click="submitForm('form')">保存修改</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import ContentHeader from "../../../../components/widge/content-header";
import { getUserInfo, updateUserInfo, getCardType } from "@/api/login";

import { getToken } from "@/utils/auth";

export default {
  components: { ContentHeader },
  data() {
    return {
      sex: "1",
      form: {
        userId: "",
        email: "",
        userId: "",
        userName: "",
        sex: "",
        mobile: "",
        wechat: "",
        code: "",
        age: null,
        job: "",
        qq: "",
        avatar: "",
        codeType: "",
        microBlog: ""
      },

      cardTypes: [],
      idCardRule: [
        {
          required: true,
          message: "请输入证件号码",
          trigger: "blur"
        },
        {
          message: "请输入正确的身份证号码！",
          pattern: /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/,
          trigger: "blur"
        }
      ],
      otherCodeRule: [
        {
          required: true,
          message: "请输入证件号码",
          trigger: "blur"
        }
      ],
      loading: false,
      isInit: false
    };
  },
  computed: {
    rules() {
      return {
        userName: [
          { required: true, message: "请输入您的姓名", trigger: "blur" }
        ],
        codeType: [
          { required: true, message: "请选择您的证件类型", trigger: "change" }
        ],
        code:
          this.form.codeType == "身份证" ? this.idCardRule : this.otherCodeRule,
        mobile: [
          {
            required: true,
            message: "请输入您的手机号码",
            trigger: "blur"
          },
          {
            pattern: /^[1][3,4,5,7,8][0-9]{9}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        email: [
          {
            min: 3,
            max: 5,
            message: "请输入正确的邮箱地址",
            trigger: "blur",
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
          }
        ],
        qq: [
          {
            pattern: /^[1-9][0-9]{4,11}$/,
            message: "请输入正确的QQ号码",
            trigger: "blur"
          }
        ]
        // age: {
        //   type: "number",
        //   trigger: "blur",
        //   message: "请输入正确的年龄"
        // }
      };
    }
  },
  methods: {
    submitForm(formName) {
      this.loading = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.updateUserInfo();
        } else {
          this.loading = false;
          this.$message({
            showClose: true,
            type: "error",
            message: "填写信息有误，请修改后再提交"
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getUserInfo() {
      getUserInfo().then(res => {
        this.form.userId = res.data.userId;
        this.form.email = res.data.email || "";
        this.form.userName = res.data.userName || "";
        this.form.sex = res.data.sex || "";
        this.form.mobile = res.data.mobile || "";
        this.form.wechat = res.data.wechat || "";
        this.form.code = res.data.identityId || "";
        this.form.age = res.data.uage || "";
        this.form.job = res.data.uword || "";
        this.form.qq = res.data.qq || "";
        this.form.codeType = res.data.idType || "";
        this.isInit = true;
        console.log(res.data.idType);
      });
    },
    getCardType() {
      getCardType().then(res => {
        this.cardTypes = res.data.recordsList;
        // this.form.codeType = this.cardTypes[0].dictName;
      });
    },
    updateUserInfo() {
      let data = {};
      data.userName = this.form.userName;
      data.idType = this.form.codeType;
      data.identityId = this.form.code;
      data.mobile = this.form.mobile;
      data.email = this.form.email;
      data.uage = this.form.age;
      data.sex = this.form.sex;
      data.uword = this.form.job;
      data.qq = this.form.qq;
      data.token = getToken();
      updateUserInfo(data).then(res => {
        if (res.flag == 1) {
          // this.$message({
          //   showClose: true,
          //   type: "success",
          //   message: "修改成功！"
          // });
          this.$notify({
            title: "成功",
            message: "修改成功！",
            duration: 3000,
            type: "success"
          });

          this.loading = false;
          this.$store.dispatch("GetUserInfo");
          setTimeout(() => {
            this.$router.push({ name: "userCenter" });
          }, 1000);
        } else if (res.flag == -1) {
          this.$message({
            type: "error",
            message: "修改失败," + res.msg
          });
        }
      });
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getUserInfo();
      this.getCardType();
    });
  }
};
</script>

<style lang="less" scoped>
.el-form {
  width: 448px;
  margin: 30px auto;
}
.el-select {
  width: 100%;
}
.el-button--primary {
  color: #fff;
  background-color: #dbad23;
  border-color: #dbad23;
}
.btn {
  text-align: center;
}
.el-button {
  padding: 12px 40px;
}
</style>
